package helipay.api.consumer;

import helipay.api.dao.AppPayDao;
import helipay.api.model.Orders;
import helipay.api.util.DateUtil;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;

import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.ObjectMessage;


public class ConsumerListener implements MessageListener {

    private Log logger = LogFactory.getLog(this.getClass());

    @Autowired
    private AppPayDao appPayDao;

    @Override
    @Async("mqExecutor")
    public void onMessage(Message message) {
       // System.out.println("i am ok");
        System.out.println("当前处理任务的线程为:" + Thread.currentThread().getName());
        ObjectMessage textMessage = (ObjectMessage) message;
        try {
            Orders orders = (Orders) textMessage.getObject();
            logger.info("开始操作数据库了哦");
            Long startTime = System.currentTimeMillis();
            appPayDao.add(orders);
            Long end = System.currentTimeMillis();
            logger.info("操作完数据库了");
            logger.info("操作数据库耗时:"+(end-startTime)+" 毫秒");
        } catch (JMSException e) {
            e.printStackTrace();
        }
    }
}
